package com.ruoyi.business.seaTotal.service.impl;

import java.util.List;

import com.ruoyi.business.buoy.domain.Buoy;
import com.ruoyi.business.buoy.mapper.BuoyMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.ruoyi.business.seaTotal.mapper.SeaTotalDataMapper;
import com.ruoyi.business.seaTotal.domain.SeaTotalData;
import com.ruoyi.business.seaTotal.service.ISeaTotalDataService;

import javax.annotation.Resource;

/**
 * 海洋数据总览Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-31
 */
@Service
public class SeaTotalDataServiceImpl implements ISeaTotalDataService 
{
    private static final Logger log = LoggerFactory.getLogger(SeaTotalDataServiceImpl.class);
    @Resource
    private SeaTotalDataMapper seaTotalDataMapper;

    @Resource
    private BuoyMapper buoyMapper;

    /**
     * 查询海洋数据总览
     * 
     * @param id 海洋数据总览主键
     * @return 海洋数据总览
     */
    @Override
    public SeaTotalData selectSeaTotalDataById(Long id)
    {
        return seaTotalDataMapper.selectSeaTotalDataById(id);
    }

    /**
     * 查询海洋数据总览列表
     * 
     * @param seaTotalData 海洋数据总览
     * @return 海洋数据总览
     */
    @Override
    public List<SeaTotalData> selectSeaTotalDataList(SeaTotalData seaTotalData)
    {
        return seaTotalDataMapper.selectSeaTotalDataList(seaTotalData);
    }

    /**
     * 新增海洋数据总览
     * 
     * @param seaTotalData 海洋数据总览
     * @return 结果
     */
    @Override
    public int insertSeaTotalData(SeaTotalData seaTotalData)
    {
        return seaTotalDataMapper.insertSeaTotalData(seaTotalData);
    }

    /**
     * 修改海洋数据总览
     * 
     * @param seaTotalData 海洋数据总览
     * @return 结果
     */
    @Override
    public int updateSeaTotalData(SeaTotalData seaTotalData)
    {
        return seaTotalDataMapper.updateSeaTotalData(seaTotalData);
    }

    /**
     * 批量删除海洋数据总览
     * 
     * @param ids 需要删除的海洋数据总览主键
     * @return 结果
     */
    @Override
    public int deleteSeaTotalDataByIds(Long[] ids)
    {
        return seaTotalDataMapper.deleteSeaTotalDataByIds(ids);
    }

    /**
     * 删除海洋数据总览信息
     * 
     * @param id 海洋数据总览主键
     * @return 结果
     */
    @Override
    public int deleteSeaTotalDataById(Long id)
    {
        return seaTotalDataMapper.deleteSeaTotalDataById(id);
    }

    @Override
    public String importSeaTotalData(List<SeaTotalData> seaTotalDataList) {
        if (CollectionUtils.isEmpty(seaTotalDataList))
        {
            throw new ServiceException("导入海洋数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SeaTotalData seaTotalData : seaTotalDataList){
            try {
                //查询buoy根据name获取ID
                Buoy buoy = new Buoy();
                buoy.setBuoyName(seaTotalData.getBuoyName());
                List<Buoy> buoyList = buoyMapper.selectBuoyList(buoy);
                if(StringUtils.isEmpty(seaTotalData.getBuoyName()) || CollectionUtils.isEmpty(buoyList)){
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("时间、").append(seaTotalData.getDate().toString()).append(" 导入失败：无浮标信息");
                    continue;
                }
                //查重
                SeaTotalData parameter =  new SeaTotalData();
                parameter.setDate(seaTotalData.getDate());
                parameter.setBuoyId(buoyList.get(0).getId());
                parameter.setImportFlag("1");
                //存在则不插入
                if(CollectionUtils.isNotEmpty(seaTotalDataMapper.selectSeaTotalDataList(parameter))){
                    failureNum++;
                    failureMsg.append("<br/>").append(failureNum).append("时间为:").append(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",seaTotalData.getDate())).append("的数据导入失败：存在相同数据");
                    continue;
                }
                seaTotalData.setBuoyId(buoyList.get(0).getId());
                seaTotalDataMapper.insertSeaTotalData(seaTotalData);
                successNum++;
                successMsg.append("<br/>").append(successNum).append("时间为: ").append(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",seaTotalData.getDate())).append(" 导入成功");
            }catch (Exception e){
                failureNum++;
                String msg = "<br/>" + failureNum + "时间:" + seaTotalData.getDate().toString() + " 导入失败：";
                failureMsg.append(msg).append(e.getMessage());
                log.error(msg, e);
            }

        }

        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else{
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
